
# $Id$
#
#  UMKC Robotics 2009.
#
#  rboyd
#
#  
#  By default, this Makefile will build skynet against your 
#  build system's libc.  If you want to build against avr-libc,
#  do the following, you need to uncomment/comment the following
#  variables:
#  		CC  (and set this to your compiler)
#  		CDEFS_LIBC
#  		CFLAGS_MCU
#  		CDEFS_F_CPU
#  		INCLUDE
#  		LD_PATH
#  		LD_VFPRINTF
#  		CFLAGS_DBG (dwarf-2=avr-libc, gdb=sys-libc, undef).
#
#  Debugging.  By default, this Makefile enables full debugging.
#  There are two variables that control debugging:
#  		CFLAGS_DBG (dwarf-2=avr-libc, gdb=sys-libc, or undefined).
#  		CDEFS_DBG (-DDBG enables debug messages w/in code.)
#
#  Float size and printf()/scanf().  By default, Float_T will be a
#  float (however your system defines that).  This affects avr-libc
#  stdio.  You should specify whether printf()/scanf() will 
#  support float conversions.
#  		LD_VFPRINTF (default, undefined for system libc)
#  		LD_VFPRINTF (float support, avr-libc: printf_flt, scanf_flt) 
#  		LD_VFPRINTF (basic support, avr-libc: printf_min, scanf_min) 
#
#  IO drivers.  By default, we build the stdout, stdin and
#  stderr io drivers.  Under avr-libc, these do nothing unless/
#  until you override their default read()/write() callbacks.
#  Under sys-libc, these devices behave just as you would
#  expect.  Uncomment/add extra drivers as necessary to the
#  variable:
#  		CDEFS_IO
#
#  Linker flags.  By default, we only link against libm.  
#  This is because we link against the system's libc - which
#  has no special support for printf()/scanf().  If you're
#  linking against avr-libc, you need to uncomment/comment
#  after doing everything else above:
#  		LD_FLAGS
#


## where is your AVR installation?
AVRHOME = /opt/avr

## your compiler.  choose wisely.
CC=gcc
#CC=avr-gcc

## Which libc do you want?
CDEFS_LIBC = -DWITH_SYSLIBC
#CDEFS_LIBC = -DWITH_AVRLIBC

## what avr chip are we using?  
CFLAGS_MCU = 
#CFLAGS_MCU = -mmcu=atmega128

## processor frequency.  
CDEFS_F_CPU = 
#CDEFS_F_CPU = -DF_CPU=16000000UL

## debugging flags for your compiler.
CFLAGS_DBG = -Wall -ggdb
#CFLAGS_DBG = -gdwarf-2
#CFLAGS_DBG = 

## optimization level
CFLAGS_OPTIMIZE = -O2

## C standard (affects vfprintf()/vfscanf() warnings 
## when using some libc's.  Don't change.
CFLAGS_CSTD = -std=c99

## how big is Float_T?
CDEFS_FLOAT_SZ = -DWITH_FLOAT
#CDEFS_FLOAT_SZ = -DWITH_DOUBLE

## debugging code built?  ok for now.
CDEFS_DEBUG = -DDBG
#CDEFS_DEBUG = 

## which io capabilities do you want?
CDEFS_IOSTDOUT = -DWITH_IOSTDOUT 
CDEFS_IOSTDIN = -DWITH_IOSTDIN
CDEFS_IOSTDERR = -DWITH_IOSTDERR
#CDEFS_IO += -DWITH_IOLCD
#CDEFS_IO += -DWITH_INT16
#CDEFS_IO += -DWITH_INT32
CDEFS_IO = $(CDEFS_IOSTDOUT) $(CDEFS_IOSTDIN) $(CDEFS_IOSTDERR)

## include path.  
INCLUDE = -I.
AVRINCLUDE = -I$(AVRHOME)/avr/include 
#AVRINCLUDE = -I$(AVRHOME)/include -I$(AVRHOME)/avr/include -I.

## ld path
LDPATH = 
#LDPATH = -L$(AVRHOME)/avr/lib -L$(AVRHOME)/lib64 -L$(AVRHOME)/lib

## which printf symbols for avr-libc?
LD_VFPRINTF = 
#LD_VFPRINTF = -u,vfprintf -u,vfscanf -lscanf_flt -lprintf_flt
#LD_VFPRINTF = -u,vfprintf -u,vfscanf -lscanf_min -lprintf_min

## math lib.  don't change this.
LD_MATH = -lm

## give ld options based on sys-libc or avr-libc.
LD_FLAGS = $(LD_MATH)
#LD_FLAGS = $(LD_VFPRINTF) $(LD_MATH) -Wl,-Map,$(PRG).map



#############################################################
#
# Best not to change things below here...
#
#
AVR_OBJCOPY = $(AVRHOME)/bin/avr-objcopy
AVR_OBJDUMP = $(AVRHOME)/bin/avr-objdump
AVR_DUDE = $(AVRHOME)/bin/avrdude
AVR_FORMAT = ihex

PRG = skynet
ERR = err
INTDEFS = intdefs
STROPS = strops
ATMEGA128 = atmega128
IOPUBLIC = iopublic
IOSTDOUT = iostdout
IOSTDERR = iostderr
IOSTDIN = iostdin
TWOSPACE = 2space
PATHCTL = path
PATHMAKER = pathmaker
RUNTIME = runtime

MAIN = main

MOD_CORE = $(ERR).o \
           $(INTDEFS).o \
           $(STROPS).o \
	   $(ATMEGA128).o \
           $(IOPUBLIC).o \
	   $(IOSTDOUT).o \
	   $(IOSTDERR).o \
	   $(IOSTDIN).o

			
MOD_NAVIGATION = $(TWOSPACE).o \
	         $(PATHCTL).o \
	         $(PATHMAKER).o

MOD_RUNTIME = $(RUNTIME).o

MOD_ROBOT = $(MOD_CORE) \
            $(MOD_NAVIGATION) \
            $(MOD_RUNTIME)


all: $(PRG)

mod_core: $(MOD_CORE)
mod_navi: $(MOD_NAVIGATION)
mod_runtime: $(MOD_RUNTIME)
mod_robot: $(MOD_ROBOT)
	
CDEFS += $(CDEFS_LIBC) \
         $(CDEFS_DEBUG) \
         $(CDEFS_FLOAT_SZ) \
	 $(CDEFS_F_CPU) \

CFLAGS += $(INCLUDE) \
          $(CFLAGS_MCU) \
	  $(CFLAGS_DBG) \
	  $(CFLAGS_OPTIMIZE) \
          $(CFLAGS_CSTD)

$(PRG): $(MOD_ROBOT) $(MAIN).o
	$(CC) $(LD_PATH) $(LD_FLAGS) -o $@ $(MOD_ROBOT) $(MAIN).o
 
$(ERR).o: $(ERR).c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(INTDEFS).o: $(INTDEFS).c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(STROPS).o: $(STROPS).c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(ATMEGA128).o: $(ATMEGA128).c
	$(CC) $(AVRINCLUDE) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(IOPUBLIC).o: $(IOPUBLIC).c
	$(CC) $(CFLAGS) $(CDEFS) $(CDEFS_IO) -c -o $@ $^

$(IOSTDOUT).o: $(IOSTDOUT).c
	$(CC) $(CFLAGS) $(CDEFS) $(CDEFS_IOSTDOUT) -c -o $@ $^

$(IOSTDIN).o: $(IOSTDIN).c
	$(CC) $(CFLAGS) $(CDEFS) $(CDEFS_IOSTDIN) -c -o $@ $^

$(IOSTDERR).o: $(IOSTDERR).c
	$(CC) $(CFLAGS) $(CDEFS) $(CDEFS_IOSTDERR) -c -o $@ $^

$(TWOSPACE).o: $(TWOSPACE).c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(PATHCTL).o: $(PATHCTL).c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(PATHMAKER).o: $(PATHMAKER).c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(RUNTIME).o: $(RUNTIME).c
	$(CC) $(AVRINCLUDE) $(CFLAGS) $(CDEFS) -c -o $@ $^

$(MAIN).o: $(MAIN).c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

bits.o: bits.c
	$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $^

bittest: $(INTDEFS).o $(STROPS).o bits.o
	$(CC) $(LD_PATH) $(LD_FLAGS) -o $@ $(STROPS).o $(INTDEFS).o bits.o

.PHONY: clean
clean:
	rm -f *.o *.elf *.map *.epp *.lss *.sym $(PRG) core*
